<template>
  <div class="app-container dict-customerType-list-wrap">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" label-width="100px">
      <el-form-item label="交易类型" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择" clearable style="width: 240px">
          <el-option v-for="dict in dict.type.customer_transaction_type" :key="dict.value" :label="dict.label"
            :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>

      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" icon="el-icon-plus" size="mini" @click="clickAddbtn"
          v-hasPermi="['config:customerType:add']">添加</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" icon="el-icon-delete" :disabled="multiple" size="mini" @click="handleDelete"
          v-hasPermi="['config:customerType:del']">删除</el-button>
      </el-col>
    </el-row>
    <el-table v-loading="loading" max-height="550" :data="dataList" border @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="40" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click="handelEdit(scope.row)" v-hasPermi="['config:customerType:edit']">编辑
          </el-button>
        </template>
      </el-table-column>
      <el-table-column label="ID" prop="code" />
      <el-table-column label="名称" prop="name"></el-table-column>
      <el-table-column label="默认" prop="defaultType">
        <template slot-scope="scope">
          <span v-if="scope.row.defaultType==0">
            <el-badge is-dot type="success" class="badge-dot-left"></el-badge>是
          </span>
          <span v-if="scope.row.defaultType==1">
            <el-badge is-dot type="warning" class="badge-dot-left"></el-badge>否
          </span>
        </template>
      </el-table-column>
      <el-table-column label="创建人员" prop="createBy" />
      <el-table-column label="创建日期" prop="createTime" width="160px" />
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />
    <el-dialog :title="ruleForm.id?'编辑客户类型信息':'添加客户类型信息'" :visible.sync="dialogVisible" width="600px">
      <div class="dialog-form-box">
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="130px" class="demo-ruleForm">
          <el-form-item label="交易类型" prop="type">
            <el-select v-model="ruleForm.type" placeholder="请选择" style="width: 85%;" @change="changetype">
              <el-option v-for="dict in dict.type.customer_transaction_type" :key="dict.value" :label="dict.label"
                :value="dict.value" />
            </el-select>
          </el-form-item>
          <el-form-item label="ID：" prop="code">
            <el-input v-model="ruleForm.code" style="width: 85%;"></el-input>
          </el-form-item>
          <el-form-item label="名称：" prop="name">
            <el-input v-model="ruleForm.name" style="width: 85%;"></el-input>
          </el-form-item>
          <el-form-item label="默认：" prop="defaultType">
            <el-select v-model="ruleForm.defaultType" placeholder="请选择" clearable style="width: 85%;">
              <el-option label="是" value="0" />
              <el-option label="否" value="1" />
            </el-select>
          </el-form-item>
          <el-form-item label="用法：">
            <el-form-item prop="accountAuthority">
              <el-checkbox v-model="ruleForm.accountAuthority">允许开账户</el-checkbox>
            </el-form-item>
            <el-form-item prop="guaranteeAuthority" v-if="!ruleForm.type || ruleForm.type==0">
              <el-checkbox v-model="ruleForm.guaranteeAuthority">允许做担保人</el-checkbox>
            </el-form-item>
            <el-form-item prop="identityInformationNeeded" v-if="!ruleForm.type || ruleForm.type==0">
              <el-checkbox v-model="ruleForm.identityInformationNeeded">需要身份资料</el-checkbox>
            </el-form-item>
            <el-form-item prop="defaultAddressShow">
              <el-checkbox v-model="ruleForm.defaultAddressShow">显示默认地址字段</el-checkbox>
            </el-form-item>
          </el-form-item>
          <el-form-item label="说明：" prop="remarks">
            <el-input type="textarea" v-model="ruleForm.remarks" style="width: 85%;"></el-input>
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="handelConfirm">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
  import {
    customerTypeList,
    customerTypeAdd,
    customerTypeEdit,
    customerTypeDelBatch
  } from '@/api/system/dict/customerType.js'

  import Treeselect from "@riophae/vue-treeselect";
  import "@riophae/vue-treeselect/dist/vue-treeselect.css";
  export default {
    name: "DictCustomerType",
    components: {
      Treeselect
    },
    dicts: ['customer_transaction_type'],
    data() {
      return {
        loading: false,
        multiple: true,
        queryParams: {
          pageNum: 1,
          pageSize: 30
        },
        dialogVisible: false,
        ruleForm: {
          accountAuthority: false,
          guaranteeAuthority: false,
          identityInformationNeeded: false,
          defaultAddressShow: false
        },
        rules: {
          type: [{
            required: true,
            message: "请选择交易类型",
            trigger: "change"
          }],
          code: [{
            required: true,
            message: "请输入ID编码",
            trigger: "change"
          }],
          name: [{
            required: true,
            message: "请输入名称",
            trigger: "change"
          }],
        },
        companyData: [],
        dataList: [],
        total: 0,
        seletIds: []

      };
    },


    created() {
      this.getList()
    },
    methods: {
      /** 转换菜单数据结构 */
      normalizer(node) {
        if (node.children && !node.children.length) {
          delete node.children;
        }
        return {
          id: node.id,
          label: node.name,
          children: node.children
        };
      },
      handleAdd() {

      },
      getList() {
        this.loading = true;
        customerTypeList(this.queryParams).then(res => {
          this.dataList = res.rows
          this.total = res.total
          this.loading = false;
        })
      },
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      handleSelectionChange(selection) {
        this.multiple = !selection.length
        this.seletIds = selection.map(item => item.id)
        this.customerTypeIDArr = selection.map(item => item.code)
      },
      //
      changetype() {
        console.log(this.ruleForm)
        this.ruleForm.accountAuthority = false
        this.ruleForm.guaranteeAuthority = false
        this.ruleForm.identityInformationNeeded = false
        this.ruleForm.defaultAddressShow = false
      },
      //批量删除产品
      handleDelete() {
        let that = this
        let ids = ''
        this.seletIds.forEach((item, i) => {
          if (this.seletIds.length - 1 == i) {
            ids += item
          } else {
            ids += item + ','
          }
        });
        this.$modal.confirm('是否确认删除客户ID为"' + this.customerTypeIDArr + '"的数据项？').then(function () {
          customerTypeDelBatch({
            ids: ids
          }).then(res => {
            that.getList();
            that.$modal.msgSuccess("删除成功");
          });
        }).catch(function () {});
      },
      // 表单重置
      reset() {
        this.resetForm("ruleForm");
        this.ruleForm = {
          id: '',
          code: '',
          name: '',
          type: '',
          defaultType: '', //默认(0、是；1、否)
          accountAuthority: false, //允许开账户(0、是)
          guaranteeAuthority: false, //允许做担保人(0、是)
          identityInformationNeeded: false, //	T文本	否需要身份资料(0、是)
          defaultAddressShow: false, //	T文本	否显示默认地址字段(0、是)
          remarks: '',
        };
      },
      //点击添加按钮弹出表单
      clickAddbtn() {
        this.dialogVisible = true
        this.reset()

      },
      //点击编辑弹出编辑表单并赋值
      handelEdit(row) {
        this.dialogVisible = true
        this.reset()
        this.ruleForm.id = row.id
        this.ruleForm.code = row.code
        this.ruleForm.name = row.name
        this.ruleForm.type = row.type
        this.ruleForm.defaultType = row.defaultType //默认(0、是；1、否)
        this.ruleForm.accountAuthority = row.accountAuthority == 0 ? true : false //允许开账户(0、是)
        this.ruleForm.guaranteeAuthority = row.guaranteeAuthority == 0 ? true : false //允许做担保人(0、是)
        this.ruleForm.identityInformationNeeded = row.identityInformationNeeded == 0 ? true : false //	T文本	否需要身份资料(0、是)
        this.ruleForm.defaultAddressShow = row.defaultAddressShow == 0 ? true : false //	T文本	否显示默认地址字段(0、是)
        this.ruleForm.remarks = row.remarks
      },
      //点击提交表单
      handelConfirm() {
        console.log(this.ruleForm)
        this.$refs["ruleForm"].validate(valid => {
          if (valid) {
            const params = {
              id: this.ruleForm.id || null,
              code: this.ruleForm.code,
              name: this.ruleForm.name,
              type: this.ruleForm.type,
              defaultType: this.ruleForm.defaultType, //默认(0、是；1、否)
              accountAuthority: this.ruleForm.accountAuthority ? 0 : 1, //允许开账户(0、是) 小组显示
              guaranteeAuthority: this.ruleForm.guaranteeAuthority ? 0 : 1, //允许做担保人(0、是)
              identityInformationNeeded: this.ruleForm.identityInformationNeeded ? 0 : 1, //	T文本	否需要身份资料(0、是)
              defaultAddressShow: this.ruleForm.defaultAddressShow ? 0 : 1, //	T文本	否显示默认地址字段(0、是) 小组显示
              remarks: this.ruleForm.remarks
            }
            if (this.ruleForm.id) {
              customerTypeEdit(params).then(res => {
                this.$modal.msgSuccess("修改成功");
                this.dialogVisible = false;
                this.getList();
              })
            } else {
              customerTypeAdd(params).then(res => {
                this.$modal.msgSuccess("添加成功");
                this.dialogVisible = false;
                this.getList();
              })
            }
          }
        })
      }
    }
  };

</script>

<style rel="stylesheet/scss" lang="scss">

</style>
